<template>
	<view>
		<view v-if="dataList && dataList.length>0">
			<view class="template-view" v-for="(item,index) in dataList" :key="index" @tap="kuaidiDetails(item)" >

				<view class="flex-between items-center">
					<view class="font-def flex-between flex-between-title">
						<view>订单号：{{item.shopbill}}</view><view>{{ payStatusDic[item.pay_status] }}</view>
					</view>
					<view class="font-def">{{item.item_name}}</view>
				</view>
				<view class="flex-around items-center" style="margin-top: 20rpx;display: flex; justify-content: space-between; align-items: center;">
					<view style="display: flex; flex-direction: column; align-items: center;">
						<view class="address-city">{{item.sender_city}}</view>
						<view class="address-name">{{item.sender}}</view>
					</view>
					<view>
						<view style="text-align: center;">
							<!-- <image src="../../../static/step.png" style="width:80rpx;height: 12rpx;"></image> -->
						</view>
						<!-- 状态码（1.待揽收 2.运输中 3.已签收 4.拒收退回 99.已取消） -->
						<view v-if="item.type_code==1" style="font-size: 22rpx;color:#828282;text-align: center;">
							待揽收"
						</view>
						<view v-if="item.type_code==2" style="font-size: 22rpx;color:#828282;text-align: center;">
							运输中
						</view>
						<view v-if="item.type_code==3" style="font-size: 22rpx;color:#828282;text-align: center;">
							已签收
						</view>
						<view v-if="item.type_code==4" style="font-size: 22rpx;color:#828282;text-align: center;">
							拒收退回
						</view>
						<view v-if="item.type_code==99" style="font-size: 22rpx;color:#828282;text-align: center;">
							已取消
						</view>
					</view>
					<view style="display: flex; flex-direction: column; align-items: center;">
						<view class="address-city">{{item.receive_city}}</view>
						<view class="address-name">{{item.receiver}}</view>
					</view>
				</view>
				<view class="flex-start items-center font-def" style="margin-top: 20rpx;">
					<!--  -->
					下单时间：{{item.create_time ? $wanlshop.timeFormatS(item.create_time) : '——'}}
				</view>
				<view class="flex-start items-center font-def" style="margin-top: 10rpx;">
					支付金额：{{item.order_price}}
				</view>
				<view v-if="item.orLogisticsNumber" class="flex-start items-center font-def" style="margin-top: 10rpx;">
					快递单号：{{item.waybill}}
				</view>
				<view class="flex-end items-center">
					<view class="flex flex-end" @click.stop>
						<u-button v-if="(item.waybill && item.shopbill && item.id) || item.state==301 || item.state==2 || item.state==3" @click="traces(item)"
							type="info" plain text="物流信息"
							:customStyle="{'height':'60rpx','width':'180rpx','margin':'0'}" shape="circle">
						</u-button>
						<u-button v-if="item.type_code==1" @click="cancelOrder(item)" type="info" plain text="取消订单"
							:customStyle="{'height':'60rpx','width':'180rpx','margin':'0','margin-left':'20rpx'}"
							shape="circle">
						</u-button>
						<u-button v-if="item.type_code==1" @click="deleteOrder(item)" type="info" plain text="删除订单"
							:customStyle="{'height':'60rpx','width':'180rpx','margin':'0','margin-left':'20rpx'}"
							shape="circle">
						</u-button>
						<!-- <u-button v-if="item.orPayMoney!=item.orReceivableMoney" @click="difference(item)"
							type="warning" plain :text="'补差价'+(item.orReceivableMoney-item.orPayMoney)+'元'"
							:customStyle="{'height':'60rpx','width':'220rpx','margin':'0','margin-left':'20rpx','color':'#f9ae3d'}"
							shape="circle">
						</u-button> -->
					</view>
				</view>
			</view>
			<u-loadmore :status="status" />
		</view>

		<view v-else style="margin-top: 200rpx;">
			<u-empty mode="order">
			</u-empty>
		</view>


	</view>
</template>

<script>
	import wanlshop_config from '@/common/config/config';
	export default {
		components: {
			//login
		},
		data() {
			return {
				isLogin: false,
				tabList: [{
						name: '全部',
						state: '1,-2,-1,120,102,103,104,301,2,3'
					},
					{
						name: '已付款',
						state: '1,120,102'
					},
					{
						name: '已接单',
						state: '103,104,301,2,3'
					},
					{
						name: '已退款',
						state: '-2,-1'
					}
				],
				current: 0,
				isload: true,
				page: 1,
				rows: 10,
				dataList: [],
				status: 'loading',
				payStatusDic: {
					0: '未支付',
					1: '已支付',
					2: '补差价',
					3: '补差价',
					9: '已取消'
				}
			}
		},
		onLoad() {
			var that = this;
				that.getList();
		},
		onShow() {
		
		},
		methods: {
			difference(item) {
				var that = this;
				that.$http.post('Order/PayOrderDifference', {
					userGuid: that.$us.getId(),
					orId: item.id
				}, function(res) {
					if (res.StatusCode == 1) {
						that.weChatPay(res.Data);
					} else {
						that.$uti.alert(res.Message);
					}
				}, '加载中...')
			},
			weChatPay(data) {
				var that = this;
				uni.requestPayment({
					provider: 'wxpay',
					nonceStr: data.nonceStr,
					package: data.package,
					paySign: data.paySign,
					signType: data.signType,
					timeStamp: data.timeStamp,
					success: (res) => {
						console.log('success:' + JSON.stringify(res));
						uni.showToast({
							title: '支付成功！',
							duration: 2000
						});
						setTimeout(function() {
							that.reload();
						}, 1000)
					},
					fail: (err) => {
						console.log('fail:' + JSON.stringify(err));
						uni.showToast({
							title: '已取消支付！',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			traces(item) {
				var that = this;
				if(!(item.waybill && item.id && item.shopbill)) {
					uni.showToast({
					    title: '订单信息有误，请选择正确的订单查询',
					    icon: 'none'
					});
					return;
				}
				let data = {
					id: item.id,
					shopbill: item.shopbill,
					waybill: item.waybill,
					itemName: item.item_name
				}
				this.$wanlshop.to(`/pages/order/logistics?data=${JSON.stringify(data)}`);
				// console.log('物流信息', json)
			},
			cancelOrder(item) {
				var that = this;
				uni.showModal({
					title: '提醒',
					content: '您是否要取消订单？',
					confirmText: '确认取消',
					success: (res)=> {
						if (res.confirm) {
							uni.showLoading({
								title: '请稍后'
							});
							that.$api.post({
								url: wanlshop_config.appurl2 + '/kuaidi/CancelKuaidi',
								data: {
									id: item.id,
									waybill: item.waybill,
									shopbill: item.shopbill
								},
								success: function(res) {
									if(res && res.code + '' === '1') {
										that.getList();
									}
									that.$wanlshop.msg(res.message);
									uni.hideLoading();						
								},
							})
						}
					}
				});
			},
			deleteOrder(item) {
				var that = this;
				uni.showModal({
					content: '您是否要删除订单？删除订单信息无法找回',
					confirmText: '确认删除',
					success: (res)=> {
						if (res.confirm) {
							uni.showLoading({
								title: '请稍后'
							});
							that.$api.get({
								url: wanlshop_config.appurl + '/wanlshop/kuaidi/delKuaidi',
								data: {
									id: item.id
								},
								success: function(res) {
									if(res) {
										that.getList();
									}
									that.$wanlshop.msg(res.msg);
									uni.hideLoading();						
								},
							})
						}
					}
				});
			},
			click(e) {
				console.log(e);
				this.current = e.index;
				this.reload();
			},
			loginClose() {
				var that = this;
				//that.isLogin = that.$us.checkLogin();
				//if (that.isLogin) {
					that.getList();
				//}
			},
			reload() {
				this.dataList = [];
				this.page = 1;
				this.isload = true;
				this.getList();
			},
			getList() {
				var that = this;
				if (!that.isload) {
					that.$wanlshop.msg('没有更多数据了');
					return;
				}
				that.status = 'loading';
				this.$api.post({
								url: '/wanlshop/kuaidi/getList',
								data: {
									userid:uni.getStorageSync('userid'),
									page: this.current_page
								},
								success: res => {
									console.log(res.data)
									this.dataList = res.data
									this.status=''
								}
							});
								
			},
			getStatus(text) {
				var status = '未知状态';
				switch (text.toString()) {
					case '0':
						status = '待支付'
						break;
					case '1':
						status = '待分配'
						break;
					case '-2':
						status = '支付失败'
						break;
					case '-1':
						status = '已取消'
						break;
					case '99':
						status = '调度异常'
						break;
					case '120':
						status = '待接单'
						break;
					case '102':
						status = '分配网点'
						break;
					case '103':
						status = '分配快递员'
						break;
					case '104':
						status = '待取件'
						break;
					case '301':
						status = '待揽件'
						break;
					case '2':
						status = '运输中'
						break;
					case '3':
						status = '已签收'
						break;
					default:
						status = '未知状态'
						break;
				}
				return status;
			},
			kuaidiDetails(order) {
				if(!(order.waybill && order.id && order.shopbill)) {
					uni.showToast({
					    title: '订单信息有误，请选择正确的订单查询',
					    icon: 'none'
					});
					return;
				}
				let data = {
					id: order.id,
					shopbill: order.shopbill,
					waybill: order.waybill,
					itemName: order.item_name
				}
				this.$wanlshop.to(`/pages/order/details?data=${JSON.stringify(data)}`);
			},
		},		
		onPullDownRefresh() {
			this.reload();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		onReachBottom() {
			this.getList();
		}
	}
</script>

<style>
	.template-view {
		width: 90%;
		margin: 0 auto;
		border-radius: 20rpx;
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;
	}

	.address-city {
		font-weight: bold;
		font-size: 32rpx;
		text-align: center;
	}

	.address-name {
		font-size: 26rpx;
		color: #999;
		text-align: center;
	}

	.font-def {
		font-size: 26rpx;
		color: #999;
	}
	.flex.flex-end {
		justify-content: flex-end;
	}
	.flex-between-title {
		display: flex;
		justify-content: space-between;
	}
</style>
